package com.restlet.spike.client;

import org.restlet.Client;
import org.restlet.util.Series;
import org.restlet.data.*;
import com.noelios.restlet.http.HttpConstants;

public class ClientRunner {

    public static void main(String[] args) throws Exception {
        // Prepare the request

        Request request = new Request(Method.POST, "http://localhost:8183/app1/users/sunlong");

        // Add the client authentication to the call

        ChallengeScheme scheme = ChallengeScheme.HTTP_BASIC;

        ChallengeResponse authentication = new ChallengeResponse(scheme, "test", "test");
        request.setChallengeResponse(authentication);

        // Add an extra http header
        Series<Parameter> headers = new Form();
        headers.add(HttpConstants.HEADER_X_HTTP_METHOD_OVERRIDE, "DELETE");
        request.getAttributes().put(HttpConstants.ATTRIBUTE_HEADERS, headers);

        // Ask to the HTTP client connector to handle the call

        Client client = new Client(Protocol.HTTP);

        Response response = client.handle(request);

        if (response.getStatus().isSuccess()) {
            // Output the response entity on the JVM console
            response.getEntity().write(System.out);
        }
    }
}
